package cn.yyx.aclservice.helper;

import cn.yyx.aclservice.entity.Permission;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 根据权限数据构建登录用户左侧菜单数据
 * </p>
 *
 * @author qydashboard
 * @since 2019-11-11
 */
public class MemuHelper {

    /**
     * 构建菜单
     *
     * @param treeNodes
     * @return
     */
    public static List<JSONObject> bulid(List<Permission> treeNodes) {
        List<JSONObject> meuns = new ArrayList<>();
        if (treeNodes.size() == 0)
            return meuns;
            Permission topNode = treeNodes.get(0);//取得跟节点
            //左侧一级菜单（即：全部权限菜单的子菜单 ）
            List<Permission> oneMeunList = topNode.getChildren();
            //第一个循环处理的是 level==2 的菜单(可能有多个)  顶级菜单 “全部权限”的level==1
            for (Permission one : oneMeunList) {
                JSONObject oneMeun = new JSONObject();
                oneMeun.put("path", one.getPath());
                oneMeun.put("component", one.getComponent());
                oneMeun.put("redirect", "noredirect");
                oneMeun.put("alwaysShow", true);
                oneMeun.put("name", "name_" + one.getId());
                oneMeun.put("hidden", false);

                JSONObject oneMeta = new JSONObject();
                oneMeta.put("title", one.getName());
                oneMeta.put("icon", one.getIcon());
                oneMeun.put("meta", oneMeta);

                List<JSONObject> children = new ArrayList<>();
                List<Permission> twoMeunList = one.getChildren();

                //第2 个循环处理当前遍历到的level==2的菜单的子菜单  子菜单的level==3
                for (Permission two : twoMeunList) {
                    JSONObject twoMeun = new JSONObject();
                    twoMeun.put("path", two.getPath());
                    twoMeun.put("component", two.getComponent());
                    twoMeun.put("name", "name_" + two.getId());
                    twoMeun.put("hidden", false);

                    JSONObject twoMeta = new JSONObject();
                    twoMeta.put("title", two.getName());
                    twoMeta.put("icon", two.getIcon());
                    twoMeun.put("meta", twoMeta);

                    children.add(twoMeun);

                    List<Permission> threeMeunList = two.getChildren();
                    //第3个循环处理的是 level==4 的菜单
                    for (Permission three : threeMeunList) {
                        if (StringUtils.isEmpty(three.getPath())) continue;

                        JSONObject threeMeun = new JSONObject();
                        threeMeun.put("path", three.getPath());
                        threeMeun.put("component", three.getComponent());
                        threeMeun.put("name", "name_" + three.getId());
                        threeMeun.put("hidden", true);
                        JSONObject threeMeta = new JSONObject();
                        threeMeta.put("icon", three.getIcon());
                        threeMeta.put("title", three.getName());
                        threeMeta.put("meta", threeMeta);

                        children.add(threeMeun);
                    }
                }
                oneMeun.put("children", children);
                meuns.add(oneMeun);
            }
        return meuns;
    }
}
